//
//  ZCompaseBottom.swift
//  swift-weibo
//
//  Created by zero on 17/3/28.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit

enum buttonTag:Int{
    
    case picture = 0
    case emoticon = 1
    case add = 2
    case mention = 3
    case camera = 4
}

class ZCompaseBottom: UIView {

    //  回调函数
    var btnClick:((buttonTag)->())?
    
    var emoticonBtn:UIButton?
    
    var isEmoji:Bool?{
        
        didSet{
            var imageName = ""
            if isEmoji! {
                // 设置按钮的image
                imageName = "compose_keyboardbutton_background"
            }else{
                
                imageName = "compose_emoticonbutton_background"
            
            }
            emoticonBtn?.setImage(UIImage(named:imageName), for: UIControlState.normal)
            emoticonBtn?.setImage(UIImage(named:"\(imageName)_highlighted"), for: UIControlState.highlighted)
        }
    }
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //  布局
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  布局
    fileprivate func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        //  图片compose_toolbar_picture
        let pictureBtn = getButton(imgName: "compose_toolbar_picture",btnTag: .picture)
        
        //  compose_emoticonbutton_background
        emoticonBtn = getButton(imgName: "compose_emoticonbutton_background",btnTag: .emoticon)
        
        //  加号compose_add_background
        let addBtn = getButton(imgName: "compose_add_background",btnTag: .add)
        
        //  @compose_mentionbutton_background
        let mentionBtn = getButton(imgName: "compose_mentionbutton_background",btnTag: .mention)
        
        //  相机compose_camerabutton_background
        let cameraBtn = getButton(imgName: "compose_camerabutton_background",btnTag: .camera)
        
        //  添加
        addSubview(pictureBtn)
        addSubview(emoticonBtn!)
        addSubview(addBtn)
        addSubview(mentionBtn)
        addSubview(cameraBtn)
        
        //  添加约束
        pictureBtn.snp.makeConstraints { (make) in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(emoticonBtn!)
        }
        
        emoticonBtn?.snp.makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureBtn.snp.trailing)
            make.width.equalTo(addBtn)
        }
        
        addBtn.snp.makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo((emoticonBtn?.snp.trailing)!)
            make.width.equalTo(mentionBtn)
        }
        
        mentionBtn.snp.makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(addBtn.snp.trailing)
            make.width.equalTo(cameraBtn)
        }
        
        cameraBtn.snp.makeConstraints { (make) in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(mentionBtn.snp.trailing)
        }
        
    }

}

//  创建 按钮
extension ZCompaseBottom{
    
    fileprivate func getButton(imgName:String,btnTag:buttonTag) ->UIButton{
        
        let button = UIButton()
        
        //  设置tag
        button.tag = btnTag.rawValue
        
        button.setImage(UIImage(named:imgName), for: .normal)
        button.setImage(UIImage(named:imgName + "_highlighted"), for: .highlighted)
        
        //  添加点击事件
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        return button
    }
    
    @objc fileprivate func buttonClick(btn:UIButton){
        
        
        btnClick?(buttonTag(rawValue: btn.tag)!)
        
    }
}
